﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms.work.SystemManager
{
    public partial class DataEmpty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo == null)
            {
                Response.Write(" <script> top.location.href='/cms/login.aspx'; </script> ");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(DataEmpty));
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string EmptyData(string pwd, string code, string data_obj)
        {
            var jsonResult = new Dictionary<string, object>();
            try
            {
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                if (userInfo == null)
                {
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", "对不起，登录超时，请退出重新登录！");
                }
                else
                {
                    string verCode = Session["VerificationCode"] == null ? "" : Session["VerificationCode"].ToString();
                    if (verCode.ToLower() == code.ToLower())
                    {
                        DataTable table = UserInfoBll.UserLogin(CommonBll.InputText(userInfo.UName, 50),
                                                                EncryptHelper.EncryptString(pwd), CommonConst.DB_Name);
                        if (table.Rows.Count == 0)
                        {
                            jsonResult.Add("isreload", false);
                            jsonResult.Add("msg", "密码错误！");
                        }
                        else
                        {
                            SystemOptionBll.EmptyData(data_obj,CommonConst.DB_Name);
                            var dicHandleLog = new Dictionary<string, object>();
                            dicHandleLog.Add("user_id", userInfo.Id);
                            dicHandleLog.Add("computername", userInfo.Computername);
                            dicHandleLog.Add("computerip", userInfo.Computerip);
                            dicHandleLog.Add("handle_note", "清空网站数据！");
                            dicHandleLog.Add("obj_guid", "All");
                            LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                            string msg = "网站数据已经被全部清空！";
                            if (data_obj.Contains("J"))
                            {
                                try
                                {
                                    var dir = Server.MapPath("/uploadFiles/");
                                    Directory.Delete(dir, true);
                                    Directory.CreateDirectory(dir);
                                    msg += "删除用户文件成功！";
                                }
                                catch (Exception)
                                {
                                    msg += "删除用户文件失败！";
                                }
                            }
                            jsonResult.Add("isreload", true);
                            jsonResult.Add("msg", msg);
                        }
                    }
                    else
                    {
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "验证码错误！");
                    }
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        /// <summary>
        /// 直接删除目录下的所有文件及文件夹(保留目录)
        /// </summary>
        /// <param name="strDir">目录地址</param>
        public static void DeleteFolder(string strDir)
        {
            if (Directory.Exists(strDir))
            {
                string[] strDirs = Directory.GetDirectories(strDir);
                string[] strFiles = Directory.GetFiles(strDir);
                foreach (string strFile in strFiles)
                {
                    File.Delete(strFile);
                }

                foreach (string strdir in strDirs)
                {
                    Directory.Delete(strdir, true);
                }
            }
            else
            {
            }
        }
    }
}
